export default class Vector3TweenStep {
    constructor(private axisEnable = {
        x: true,
        y: true,
        z: true
    }) {

    }

    /**
     * 增量
     */
    add?: Vector3;
    /**
     * 目标
     */
    target?: Vector3
    /**
     * 时长
     */
    time = 0.2

    /**
     * 剩余时长
     */
    remainTime = 0.2
    /**
     * 当前值
     */
    currentValue?: Vector3
    tweenStepVector3Play(d: number, target: Vector3, teleportEffect?: () => void) {
        const cts = this;
        if (cts.currentValue === target) {//完成
            cts.remainTime = cts.time;
            return target
        } else if (!cts.currentValue) {//初始化当前值
            cts.currentValue = target
            cts.target = target;
            return target
        } else if (cts.target !== target) {//目标值变化
            if (cts.target && target.sub(cts.target).Magnitude > 20) {
                if (teleportEffect) {
                    teleportEffect();
                }
                cts.remainTime = cts.time;
                cts.target = target;
                return target
            }
            cts.remainTime = cts.time;//重置剩余时间
            cts.target = target//重置剩余值
            cts.add = target.sub(cts.currentValue).div(cts.remainTime);//重新计算增量
        }
        cts.remainTime -= d
        if (cts.remainTime < 0) {
            cts.remainTime = 0;
            cts.currentValue = cts.target
            return cts.currentValue;
        }
        if (cts.add) {
            const result = cts.currentValue.add(cts.add.mul(d));
            let [x, y, z] = [target.X, target.Y, target.Z]
            if (this.axisEnable.x) {
                x = result.X
            }
            if (this.axisEnable.y) {
                y = result.Y
            }
            if (this.axisEnable.z) {
                z = result.Z
            }
            cts.currentValue = new Vector3(x, y, z)
            return cts.currentValue;
        }
        return target

    }
}
